<?php
/**
 *
 * @since 2011-06-11
 * @author Cmd
 * @copyright (c) 2011, Cmd
 */

class knowledgeApp extends app
{
	function __construct(){
		$this->_init();
		$this->login_need();
	}

	public function index(){

		$tagname = $this->getData('get/tag_name');
		$tagname = intval($tagname);
		$knowledgemod = & $this->getModel('knowledge');
		$list = $knowledgemod->get_knowledges(10000, 1);
		if(empty($tagname)){
			return array('list' => $list);
		}
		else{
			switch ($tagname){
				case 1:
					$list = $knowledgemod->get_knowledges_by_main_tag('人力', 100000, 1);
					break;
				case 2:
					$list = $knowledgemod->get_knowledges_by_main_tag('财务', 100000, 1);
					break;
				case 3:
					$list = $knowledgemod->get_knowledges_by_main_tag('通用', 100000, 1);
					break;
				case 4:
					$list = $knowledgemod->get_knowledges_by_user_id($this->user_id, 100000, 1);
					break;

			}
				
			return array('list' => $list);
		}
		exit;
	}

	public function search(){
		$data = $this->getData('get/search');

		if(empty($data) || strlen($data) < 1){
			go_to('/knowledge/index', '什么也没有输入');
		}

		$knowledgemod = $this->getModel('knowledge');
		$r = $knowledgemod->search_knowledges_by_keyword($data,10000,1);
		return array('keyword' => $data,'result' => $r);

	}

	public function view(){

		$knowledge_id = $this->getData('get/id');
		$knowledge_id = intval($knowledge_id);
		if(!$knowledge_id){
			go_to('/knowledge/index', '没有传递参数');
		}
		$knowledgemod = $this->getModel('knowledge');
		$kg = $knowledgemod->get_knowledge_by_knowledge_id($knowledge_id);

		$kgitems = $knowledgemod->get_knowledge_items_by_knowledge_id($knowledge_id,1000000,1);

		if(empty($kg)){
			go_to('/knowledge/index', '没有获取到数据');
		}
		$data = $this->getData('post');
		if(empty($data)){
			return array('kg' => $kg, 'items' => $kgitems, 'kid' => $knowledge_id);
		}

		return array();
	}

	public function attachment(){
		$filename = $this->getData('get/qqfile');
		$id = & $this->getData('get/kid');
		if(empty($filename)){
			return array('kid' => intval($id));
		}else{
			$file = file_get_contents("php://input");
			if(empty($file)){
				return array('error' => '文件上传失败');
			}
			$file_size = strlen($file);
			if($id == 0){
				return array('error' => '文件过期，请重新更新');
			}
			$attobj = &$this->getModel('knowledge');
			$know = $attobj->get_knowledge_by_knowledge_id($id);
			$file_path = $attobj->save_file($file, $filename);
			if($file_path){
				$data = serialize(array('name' => $filename, 'size' => $file_size, 'url' => $file_path));
				$attobj->add_knowledge_item($id, $this->user_id, 1, $data);
			}
			$model = & $this->getModel('model');
			$model_id = $model->get_model_id('know', $id);
			if($model_id >0){
				$feedobj = & $this->getModel('feed');
				$feedobj->add($this->user_id, "在知识【{$know['subject']}】上传了文件：【{$filename}】", $model_id);
			}
			return array('success' => true);
		}
	}

	public function download(){
		$aid = $this->getData('get/attchid');
		$attaobj = & $this->getModel('knowledge');
		$att = $attaobj->get_knowledge_item_by_knowledge_item_id($aid);
		if(empty($att)){
			$this->message('文件已经不存在', 'error');
		}
		$file = unserialize($att['data']);
		$file_path = ROOT.$file['url'];
		if(!is_file($file_path)){
			$this->message('文件已经不存在', 'error');
		}
		$file_type = $attaobj->get_type_by_file_name($file['name']);
		$MIMETypes = array(
		    'ez'         => 'application/andrew-inset',    
		    'hqx'        => 'application/mac-binhex40',    
		    'cpt'        => 'application/mac-compactpro',    
		    'doc'        => 'application/msword',    
		    'bin'        => 'application/octet-stream',    
		    'dms'        => 'application/octet-stream',    
		    'lha'        => 'application/octet-stream',    
		    'lzh'        => 'application/octet-stream',    
		    'exe'        => 'application/octet-stream',    
		    'class'      => 'application/octet-stream',    
		    'so'         => 'application/octet-stream',    
		    'dll'        => 'application/octet-stream',    
		    'oda'        => 'application/oda',    
		    'pdf'        => 'application/pdf',    
		    'ai'         => 'application/postscrīpt',    
		    'eps'        => 'application/postscrīpt',    
		    'ps'         => 'application/postscrīpt',    
		    'smi'        => 'application/smil',    
		    'smil'       => 'application/smil',    
		    'mif'        => 'application/vnd.mif',    
		    'xls'        => 'application/vnd.ms-excel',    
		    'ppt'        => 'application/vnd.ms-powerpoint',    
		    'wbxml'      => 'application/vnd.wap.wbxml',    
		    'wmlc'       => 'application/vnd.wap.wmlc',    
		    'wmlsc'      => 'application/vnd.wap.wmlscrīptc',    
		    'bcpio'      => 'application/x-bcpio',    
		    'vcd'        => 'application/x-cdlink',    
		    'pgn'        => 'application/x-chess-pgn',    
		    'cpio'       => 'application/x-cpio',    
		    'csh'        => 'application/x-csh',    
		    'dcr'        => 'application/x-director',    
		    'dir'        => 'application/x-director',    
		    'dxr'        => 'application/x-director',    
		    'dvi'        => 'application/x-dvi',    
		    'spl'        => 'application/x-futuresplash',    
		    'gtar'       => 'application/x-gtar',    
		    'hdf'        => 'application/x-hdf',    
		    'js'         => 'application/x-javascrīpt',    
		    'skp'        => 'application/x-koan',    
		    'skd'        => 'application/x-koan',    
		    'skt'        => 'application/x-koan',    
		    'skm'        => 'application/x-koan',    
		    'latex'      => 'application/x-latex',    
		    'nc'         => 'application/x-netcdf',    
		    'cdf'        => 'application/x-netcdf',    
		    'sh'         => 'application/x-sh',    
		    'shar'       => 'application/x-shar',    
		    'swf'        => 'application/x-shockwave-flash',    
		    'sit'        => 'application/x-stuffit',    
		    'sv4cpio'    => 'application/x-sv4cpio',    
		    'sv4crc'     => 'application/x-sv4crc',    
		    'tar'        => 'application/x-tar',    
		    'tcl'        => 'application/x-tcl',    
		    'tex'        => 'application/x-tex',    
		    'texinfo'    => 'application/x-texinfo',    
		    'texi'       => 'application/x-texinfo',    
		    't'          => 'application/x-troff',    
		    'tr'         => 'application/x-troff',    
		    'roff'       => 'application/x-troff',    
		    'man'        => 'application/x-troff-man',    
		    'me'         => 'application/x-troff-me',    
		    'ms'         => 'application/x-troff-ms',    
		    'ustar'      => 'application/x-ustar',    
		    'src'        => 'application/x-wais-source',    
		    'xhtml'      => 'application/xhtml+xml',    
		    'xht'        => 'application/xhtml+xml',    
		    'zip'        => 'application/zip',    
		    'au'         => 'audio/basic',    
		    'snd'        => 'audio/basic',    
		    'mid'        => 'audio/midi',    
		    'midi'       => 'audio/midi',    
		    'kar'        => 'audio/midi',    
		    'mpga'       => 'audio/mpeg',    
		    'mp2'        => 'audio/mpeg',    
		    'mp3'        => 'audio/mpeg',    
		    'aif'        => 'audio/x-aiff',    
		    'aiff'       => 'audio/x-aiff',    
		    'aifc'       => 'audio/x-aiff',    
		    'm3u'        => 'audio/x-mpegurl',    
		    'ram'        => 'audio/x-pn-realaudio',    
		    'rm'         => 'audio/x-pn-realaudio',    
		    'rpm'        => 'audio/x-pn-realaudio-plugin',    
		    'ra'         => 'audio/x-realaudio',    
		    'wav'        => 'audio/x-wav',    
		    'pdb'        => 'chemical/x-pdb',    
		    'xyz'        => 'chemical/x-xyz',    
		    'bmp'        => 'image/bmp',    
		    'gif'        => 'image/gif',    
		    'ief'        => 'image/ief',    
		    'jpeg'       => 'image/jpeg',    
		    'jpg'        => 'image/jpeg',    
		    'jpe'        => 'image/jpeg',    
		    'png'        => 'image/png',    
		    'tiff'       => 'image/tiff',    
		    'tif'        => 'image/tiff',    
		    'djvu'       => 'image/vnd.djvu',    
		    'djv'        => 'image/vnd.djvu',    
		    'wbmp'       => 'image/vnd.wap.wbmp',    
		    'ras'        => 'image/x-cmu-raster',    
		    'pnm'        => 'image/x-portable-anymap',    
		    'pbm'        => 'image/x-portable-bitmap',    
		    'pgm'        => 'image/x-portable-graymap',    
		    'ppm'        => 'image/x-portable-pixmap',    
		    'rgb'        => 'image/x-rgb',    
		    'xbm'        => 'image/x-xbitmap',    
		    'xpm'        => 'image/x-xpixmap',    
		    'xwd'        => 'image/x-xwindowdump',    
		    'igs'        => 'model/iges',    
		    'iges'       => 'model/iges',    
		    'msh'        => 'model/mesh',    
		    'mesh'       => 'model/mesh',    
		    'silo'       => 'model/mesh',    
		    'wrl'        => 'model/vrml',    
		    'vrml'       => 'model/vrml',    
		    'css'        => 'text/css',    
		    'html'       => 'text/html',    
		    'htm'        => 'text/html',    
		    'asc'        => 'text/plain',    
		    'txt'        => 'text/plain',    
		    'rtx'        => 'text/richtext',    
		    'rtf'        => 'text/rtf',    
		    'sgml'       => 'text/sgml',    
		    'sgm'        => 'text/sgml',    
		    'tsv'        => 'text/tab-separated-values',    
		    'wml'        => 'text/vnd.wap.wml',    
		    'wmls'       => 'text/vnd.wap.wmlscrīpt',    
		    'etx'        => 'text/x-setext',    
		    'xsl'        => 'text/xml',    
		    'xml'        => 'text/xml',    
		    'mpeg'       => 'video/mpeg',    
		    'mpg'        => 'video/mpeg',    
		    'mpe'        => 'video/mpeg',    
		    'qt'         => 'video/quicktime',    
		    'mov'        => 'video/quicktime',    
		    'mxu'        => 'video/vnd.mpegurl',    
		    'avi'        => 'video/x-msvideo',    
		    'movie'      => 'video/x-sgi-movie',    
		    'ice'        => 'x-conference/x-cooltalk',    
		);
		$file_ext = substr($file['file_type'],1);
		$file_type = $MIMETypes[$file_ext] ? $MIMETypes[$file_ext] : 'application/octet-stream';

		//header('Cache-control: max-age=31536000');
		//header('Expires: ' . gmdate('D, d M Y H:i:s', time()+31536000) . ' GMT');
		//header('Last-Modified: ' . gmdate('D, d M Y H:i:s' , filemtime($file_path) . ' GMT'));
		header('Content-Encoding: none');
		header('Content-type: ' . $file_type);
		header('Content-Disposition: attachment; filename=' . $file['name']);
		header('Content-Length: ' . filesize($file_path));
		$fp = fopen($file_path, 'rb');
		//$content = ob_get_clean();
		//if(!empty($content)){
		//echo strlen($content);
		//exit($content);
		//}
		//exit('3');
		ob_end_clean();
		fpassthru($fp);
		fclose($fp);
		exit;
	}

	public function add(){
		$dataget = $this->getData('get/keyword');
		$datapost = $this->getData('post');
			
		$group = & $this->getModel('group');
		if(empty($datapost)){
			return array('keyword' => $dataget ,'groups'=> $group->get_groups(1000000,1));
		}
			
		if($datapost['forum-subject'] == ''){
			$this->inner_msg('没有关键字');
			return array('groups'=> $group->get_groups(1000000,1));
		}
		if($datapost['forum-message'] == ''){
			$this->inner_msg('没有描述');
			return array('groups'=> $group->get_groups(1000000,1));
		}
		$datapost['DocGroup'] = intval($datapost['DocGroup']);
		$datapost['EditGroup'] = intval($datapost['EditGroup']);
		$datapost['ViewGroup'] = intval($datapost['ViewGroup']);

		$datapost['flag_item_add_group_id'] = intval($datapost['flag_item_add_group_id']);
		$datapost['flag_item_view_group_id'] = intval($datapost['flag_item_view_group_id']);
		
		

		if(!in_array($datapost['DocGroup'], array(0, 1 , 2))){
			$this->inner_msg('主题归档有误');
			return array('keyword' => $dataget ,'groups'=> $group->get_groups(1000000,1));
		}
		if(!in_array($datapost['EditGroup'], array(0, 1 ,2))){
			$this->inner_msg('主题查看权限有误');
			return array('keyword' => $dataget ,'groups'=> $group->get_groups(1000000,1));
		}
		if(!in_array($datapost['ViewGroup'], array(0, 1 ,2))){
			$this->inner_msg('主题修改权限有误');
			return array('keyword' => $dataget ,'groups'=> $group->get_groups(1000000,1));
		}

		$knowledgemod = $this->getModel('knowledge');
		$id = $knowledgemod->add($this->user_id, $datapost['forum-subject'], $datapost['forum-message'], $datapost['EditGroup'], $datapost['flag_item_add_group_id'], $datapost['ViewGroup'], $datapost['flag_item_view_group_id'], $datapost['DocGroup']);
		$model = & $this->getModel('model');
		$model_id = $model->create($this->user_id, $this->user['username'], $id, 'know', $datapost['forum-subject'], $datapost['forum-message']);
		if($model_id >0){
			$feedobj = & $this->getModel('feed');
			$feedobj->add($this->user_id, "创建了新知识：<a href='/model/view?modelid={$model_id}'>{$datapost['forum-subject']}</a> {$datapost['forum-message']}", $model_id);
		}
		if($id > 0){
			go_to('/knowledge/index', '添加成功');
		}else{
			go_to('/knowledge/index', '添加失败');
		}

	}

	public function addknowledge(){

		$datapost = $this->getData('post');

		if(empty($datapost)){
			return array();
		}
		if($datapost['forum-message'] == ''){
			return array('msg' => '没有内容');
		}
		if($datapost['knowledge_id'] == ''){
			return array('msg' => '没有添加的目标');
		}
		$id = intval($datapost['knowledge_id']);
		$knowledgemod = $this->getModel('knowledge');
		$id = $knowledgemod->add_knowledge_item($id, $this->user_id, 0, $datapost['forum-message']);
		if($id > 0){
			return array('msg' => '添加成功');
		}else{
			return array('msg' => '添加失败');
		}

		return array();
	}


}
